###### Appendix: Figure C3
###### Robustness to Moving Average of Dependent and Independent Variables
.rs.restartR()
gc()
rm(list = ls())
set.seed(12345)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # Note: if you are not using R Studio this command will not work, set WD to source file location manually

source("functions.R")
source("data/working_eba_function_current.R") 
require(pacman)
pacman::p_load(plm, sandwich, clubSandwich, lmtest, 
               ggplot2, numbers, dplyr, Hmisc, formattable, 
               htmltools, webshot, ggpubr, panelView, fixest, plyr, 
               scales, DataCombine)
require(webshot)
require(stringr)
date <- paste(data.table::tstrsplit(Sys.Date(), "-")[c(2:3, 1)], collapse = "")
load("data/cleaned/vat_panel_cleaned15Nov.RData")

 

################################################################################
### Appendix Figure C3: 5 year non-missing moving average
################################################################################
dfAccma <- dfMerge %>% 
    dplyr::select(ccode, 
           year, 
           acc_vert) %>%
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_acc_vert = dplyr::lag(acc_vert)) %>% 
    dplyr::mutate(lag2_acc_vert = dplyr::lag(lag1_acc_vert)) %>% 
    dplyr::mutate(lead1_acc_vert = dplyr::lead(acc_vert)) %>% 
    dplyr::mutate(lead2_acc_vert = dplyr::lead(lead1_acc_vert)) %>% 
    dplyr::mutate(lag1_acc_vert_ind = ifelse(is.na(lag1_acc_vert), 0, 1)) %>% 
    dplyr::mutate(lag2_acc_vert_ind = ifelse(is.na(lag2_acc_vert), 0, 1)) %>% 
    dplyr::mutate(lead1_acc_vert_ind = ifelse(is.na(lead1_acc_vert), 0, 1)) %>% 
    dplyr::mutate(lead2_acc_vert_ind = ifelse(is.na(lead2_acc_vert), 0, 1)) %>% 
    dplyr::mutate(acc_vert_ind = ifelse(is.na(acc_vert), 0, 1))


dfAccma <- dfAccma %>%
    mutate(row_pres = select(., lag1_acc_vert_ind:acc_vert_ind) %>% 
               rowSums(na.rm = TRUE)) %>% 
    dplyr::mutate(row_pres = replace(row_pres, row_pres >= 0, row_pres - ccode)) %>% 
    dplyr::mutate(lag2_acc_vert = replace(lag2_acc_vert, is.na(lag2_acc_vert), 0)) %>% 
    dplyr::mutate(lag1_acc_vert = replace(lag1_acc_vert, is.na(lag1_acc_vert), 0)) %>% 
    dplyr::mutate(acc_vert = replace(acc_vert, is.na(acc_vert), 0)) %>% 
    dplyr::mutate(lead1_acc_vert = replace(lead1_acc_vert, is.na(lead1_acc_vert), 0)) %>% 
    dplyr::mutate(lead2_acc_vert = replace(lead2_acc_vert, is.na(lead2_acc_vert), 0))
    

dfAccma$accma <- (dfAccma$lag2_acc_vert + 
                      dfAccma$lag1_acc_vert + 
                      dfAccma$acc_vert + 
                      dfAccma$lead1_acc_vert + 
                      dfAccma$lead2_acc_vert)/dfAccma$row_pres
dfAccmaselect <- dfAccma %>% 
    dplyr::mutate(accma = replace(accma, is.nan(accma), NA)) %>% 
    dplyr::select(ccode, 
           year, 
           accma) 

dfAccmarename <- dfAccmaselect %>% 
    dplyr::rename(acc_vert = accma) %>%
    dplyr::ungroup()

dfAccmaclean <- dfAccmarename 
    

dfV2xcorr <- dfMerge %>% 
    dplyr::select(ccode, 
                  year, 
                  v2x_corr) %>%
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_v2x_corr = dplyr::lag(v2x_corr)) %>% 
    dplyr::mutate(lag2_v2x_corr = dplyr::lag(lag1_v2x_corr)) %>% 
    dplyr::mutate(lead1_v2x_corr = dplyr::lead(v2x_corr)) %>% 
    dplyr::mutate(lead2_v2x_corr = dplyr::lead(lead1_v2x_corr)) %>% 
    dplyr::mutate(lag1_v2x_corr_ind = ifelse(is.na(lag1_v2x_corr), 0, 1)) %>% 
    dplyr::mutate(lag2_v2x_corr_ind = ifelse(is.na(lag2_v2x_corr), 0, 1)) %>% 
    dplyr::mutate(lead1_v2x_corr_ind = ifelse(is.na(lead1_v2x_corr), 0, 1)) %>% 
    dplyr::mutate(lead2_v2x_corr_ind = ifelse(is.na(lead2_v2x_corr), 0, 1)) %>% 
    dplyr::mutate(v2x_corr_ind = ifelse(is.na(v2x_corr), 0, 1))


dfV2xcorr <- dfV2xcorr %>%
    mutate(row_pres = select(., lag1_v2x_corr_ind:v2x_corr_ind) %>% 
               rowSums(na.rm = TRUE)) %>% 
    dplyr::mutate(row_pres = replace(row_pres, row_pres >= 0, row_pres - ccode)) %>% 
    dplyr::mutate(lag2_v2x_corr = replace(lag2_v2x_corr, is.na(lag2_v2x_corr), 0)) %>% 
    dplyr::mutate(lag1_v2x_corr = replace(lag1_v2x_corr, is.na(lag1_v2x_corr), 0)) %>% 
    dplyr::mutate(v2x_corr = replace(v2x_corr, is.na(v2x_corr), 0)) %>% 
    dplyr::mutate(lead1_v2x_corr = replace(lead1_v2x_corr, is.na(lead1_v2x_corr), 0)) %>% 
    dplyr::mutate(lead2_v2x_corr = replace(lead2_v2x_corr, is.na(lead2_v2x_corr), 0))


dfV2xcorr$v2xcorr <- (dfV2xcorr$lag2_v2x_corr + 
                      dfV2xcorr$lag1_v2x_corr + 
                      dfV2xcorr$v2x_corr + 
                      dfV2xcorr$lead1_v2x_corr + 
                      dfV2xcorr$lead2_v2x_corr)/dfV2xcorr$row_pres
dfV2xcorrselect <- dfV2xcorr %>% 
    dplyr::mutate(v2xcorr = replace(v2xcorr, is.nan(v2xcorr), NA)) %>% 
    dplyr::select(ccode, 
                  year, 
                  v2xcorr) 

dfV2xcorrrename <- dfV2xcorrselect %>% 
    dplyr::rename(v2x_corr = v2xcorr) %>%
    dplyr::ungroup()

dfV2xcorrclean <- dfV2xcorrrename 

dfCombindirect <- dfMerge %>% 
    dplyr::select(ccode, 
                  year, 
                  comb_indirect) %>%
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_comb_indirect = dplyr::lag(comb_indirect)) %>% 
    dplyr::mutate(lag2_comb_indirect = dplyr::lag(lag1_comb_indirect)) %>% 
    dplyr::mutate(lead1_comb_indirect = dplyr::lead(comb_indirect)) %>% 
    dplyr::mutate(lead2_comb_indirect = dplyr::lead(lead1_comb_indirect)) %>% 
    dplyr::mutate(lag1_comb_indirect_ind = ifelse(is.na(lag1_comb_indirect), 0, 1)) %>% 
    dplyr::mutate(lag2_comb_indirect_ind = ifelse(is.na(lag2_comb_indirect), 0, 1)) %>% 
    dplyr::mutate(lead1_comb_indirect_ind = ifelse(is.na(lead1_comb_indirect), 0, 1)) %>% 
    dplyr::mutate(lead2_comb_indirect_ind = ifelse(is.na(lead2_comb_indirect), 0, 1)) %>% 
    dplyr::mutate(comb_indirect_ind = ifelse(is.na(comb_indirect), 0, 1))


dfCombindirect <- dfCombindirect %>%
    mutate(row_pres = select(., lag1_comb_indirect_ind:comb_indirect_ind) %>% 
               rowSums(na.rm = TRUE)) %>% 
    dplyr::mutate(row_pres = replace(row_pres, row_pres >= 0, row_pres - ccode)) %>% 
    dplyr::mutate(lag2_comb_indirect = replace(lag2_comb_indirect, is.na(lag2_comb_indirect), 0)) %>% 
    dplyr::mutate(lag1_comb_indirect = replace(lag1_comb_indirect, is.na(lag1_comb_indirect), 0)) %>% 
    dplyr::mutate(comb_indirect = replace(comb_indirect, is.na(comb_indirect), 0)) %>% 
    dplyr::mutate(lead1_comb_indirect = replace(lead1_comb_indirect, is.na(lead1_comb_indirect), 0)) %>% 
    dplyr::mutate(lead2_comb_indirect = replace(lead2_comb_indirect, is.na(lead2_comb_indirect), 0))


dfCombindirect$combindirect <- (dfCombindirect$lag2_comb_indirect + 
                      dfCombindirect$lag1_comb_indirect + 
                      dfCombindirect$comb_indirect + 
                      dfCombindirect$lead1_comb_indirect + 
                      dfCombindirect$lead2_comb_indirect)/dfCombindirect$row_pres
dfCombindirectselect <- dfCombindirect %>% 
    dplyr::mutate(combindirect = replace(combindirect, is.nan(combindirect), NA)) %>% 
    dplyr::select(ccode, 
                  year, 
                  combindirect) 

dfCombindirectrename <- dfCombindirectselect %>% 
    dplyr::rename(comb_indirect = combindirect) %>%
    dplyr::ungroup()

dfCombindirectclean <- dfCombindirectrename %>% 
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_comb_indirect = lag(comb_indirect))

dfDirect <- dfMerge %>% 
    dplyr::select(ccode, 
                  year, 
                  direct_ex_sc_ex_rt) %>%
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_direct_ex_sc_ex_rt = dplyr::lag(direct_ex_sc_ex_rt)) %>% 
    dplyr::mutate(lag2_direct_ex_sc_ex_rt = dplyr::lag(lag1_direct_ex_sc_ex_rt)) %>% 
    dplyr::mutate(lead1_direct_ex_sc_ex_rt = dplyr::lead(direct_ex_sc_ex_rt)) %>% 
    dplyr::mutate(lead2_direct_ex_sc_ex_rt = dplyr::lead(lead1_direct_ex_sc_ex_rt)) %>% 
    dplyr::mutate(lag1_direct_ex_sc_ex_rt_ind = ifelse(is.na(lag1_direct_ex_sc_ex_rt), 0, 1)) %>% 
    dplyr::mutate(lag2_direct_ex_sc_ex_rt_ind = ifelse(is.na(lag2_direct_ex_sc_ex_rt), 0, 1)) %>% 
    dplyr::mutate(lead1_direct_ex_sc_ex_rt_ind = ifelse(is.na(lead1_direct_ex_sc_ex_rt), 0, 1)) %>% 
    dplyr::mutate(lead2_direct_ex_sc_ex_rt_ind = ifelse(is.na(lead2_direct_ex_sc_ex_rt), 0, 1)) %>% 
    dplyr::mutate(direct_ex_sc_ex_rt_ind = ifelse(is.na(direct_ex_sc_ex_rt), 0, 1))


dfDirect <- dfDirect %>%
    mutate(row_pres = select(., lag1_direct_ex_sc_ex_rt_ind:direct_ex_sc_ex_rt_ind) %>% 
               rowSums(na.rm = TRUE)) %>% 
    dplyr::mutate(row_pres = replace(row_pres, row_pres >= 0, row_pres - ccode)) %>% 
    dplyr::mutate(lag2_direct_ex_sc_ex_rt = replace(lag2_direct_ex_sc_ex_rt, is.na(lag2_direct_ex_sc_ex_rt), 0)) %>% 
    dplyr::mutate(lag1_direct_ex_sc_ex_rt = replace(lag1_direct_ex_sc_ex_rt, is.na(lag1_direct_ex_sc_ex_rt), 0)) %>% 
    dplyr::mutate(direct_ex_sc_ex_rt = replace(direct_ex_sc_ex_rt, is.na(direct_ex_sc_ex_rt), 0)) %>% 
    dplyr::mutate(lead1_direct_ex_sc_ex_rt = replace(lead1_direct_ex_sc_ex_rt, is.na(lead1_direct_ex_sc_ex_rt), 0)) %>% 
    dplyr::mutate(lead2_direct_ex_sc_ex_rt = replace(lead2_direct_ex_sc_ex_rt, is.na(lead2_direct_ex_sc_ex_rt), 0))


dfDirect$direct <- (dfDirect$lag2_direct_ex_sc_ex_rt + 
                      dfDirect$lag1_direct_ex_sc_ex_rt + 
                      dfDirect$direct_ex_sc_ex_rt + 
                      dfDirect$lead1_direct_ex_sc_ex_rt + 
                      dfDirect$lead2_direct_ex_sc_ex_rt)/dfDirect$row_pres
dfDirectselect <- dfDirect %>% 
    dplyr::mutate(direct = replace(direct, is.nan(direct), NA)) %>% 
    dplyr::select(ccode, 
                  year, 
                  direct) 

dfDirectrename <- dfDirectselect %>% 
    dplyr::rename(direct_ex_sc_ex_rt = direct) %>%
    dplyr::ungroup()

dfDirectclean <- dfDirectrename %>% 
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_direct_ex_sc_ex_rt = lag(direct_ex_sc_ex_rt))

dfRev <- dfMerge %>% 
    dplyr::select(ccode, 
                  year, 
                  rev_ex_gr_ex_sc) %>%
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_rev_ex_gr_ex_sc = dplyr::lag(rev_ex_gr_ex_sc)) %>% 
    dplyr::mutate(lag2_rev_ex_gr_ex_sc = dplyr::lag(lag1_rev_ex_gr_ex_sc)) %>% 
    dplyr::mutate(lead1_rev_ex_gr_ex_sc = dplyr::lead(rev_ex_gr_ex_sc)) %>% 
    dplyr::mutate(lead2_rev_ex_gr_ex_sc = dplyr::lead(lead1_rev_ex_gr_ex_sc)) %>% 
    dplyr::mutate(lag1_rev_ex_gr_ex_sc_ind = ifelse(is.na(lag1_rev_ex_gr_ex_sc), 0, 1)) %>% 
    dplyr::mutate(lag2_rev_ex_gr_ex_sc_ind = ifelse(is.na(lag2_rev_ex_gr_ex_sc), 0, 1)) %>% 
    dplyr::mutate(lead1_rev_ex_gr_ex_sc_ind = ifelse(is.na(lead1_rev_ex_gr_ex_sc), 0, 1)) %>% 
    dplyr::mutate(lead2_rev_ex_gr_ex_sc_ind = ifelse(is.na(lead2_rev_ex_gr_ex_sc), 0, 1)) %>% 
    dplyr::mutate(rev_ex_gr_ex_sc_ind = ifelse(is.na(rev_ex_gr_ex_sc), 0, 1))


dfRev <- dfRev %>%
    mutate(row_pres = select(., lag1_rev_ex_gr_ex_sc_ind:rev_ex_gr_ex_sc_ind) %>% 
               rowSums(na.rm = TRUE)) %>% 
    dplyr::mutate(row_pres = replace(row_pres, row_pres >= 0, row_pres - ccode)) %>% 
    dplyr::mutate(lag2_rev_ex_gr_ex_sc = replace(lag2_rev_ex_gr_ex_sc, is.na(lag2_rev_ex_gr_ex_sc), 0)) %>% 
    dplyr::mutate(lag1_rev_ex_gr_ex_sc = replace(lag1_rev_ex_gr_ex_sc, is.na(lag1_rev_ex_gr_ex_sc), 0)) %>% 
    dplyr::mutate(rev_ex_gr_ex_sc = replace(rev_ex_gr_ex_sc, is.na(rev_ex_gr_ex_sc), 0)) %>% 
    dplyr::mutate(lead1_rev_ex_gr_ex_sc = replace(lead1_rev_ex_gr_ex_sc, is.na(lead1_rev_ex_gr_ex_sc), 0)) %>% 
    dplyr::mutate(lead2_rev_ex_gr_ex_sc = replace(lead2_rev_ex_gr_ex_sc, is.na(lead2_rev_ex_gr_ex_sc), 0))


dfRev$rev <- (dfRev$lag2_rev_ex_gr_ex_sc + 
                      dfRev$lag1_rev_ex_gr_ex_sc + 
                      dfRev$rev_ex_gr_ex_sc + 
                      dfRev$lead1_rev_ex_gr_ex_sc + 
                      dfRev$lead2_rev_ex_gr_ex_sc)/dfRev$row_pres
dfRevselect <- dfRev %>% 
    dplyr::mutate(rev = replace(rev, is.nan(rev), NA)) %>% 
    dplyr::select(ccode, 
                  year, 
                  rev) 

dfRevrename <- dfRevselect %>% 
    dplyr::rename(rev_ex_gr_ex_sc = rev) %>%
    dplyr::ungroup()

dfRevclean <- dfRevrename %>% 
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_rev_ex_gr_ex_sc = lag(rev_ex_gr_ex_sc))

dfNontax <- dfMerge %>% 
    dplyr::select(ccode, 
                  year, 
                  nontax) %>%
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_nontax = dplyr::lag(nontax)) %>% 
    dplyr::mutate(lag2_nontax = dplyr::lag(lag1_nontax)) %>% 
    dplyr::mutate(lead1_nontax = dplyr::lead(nontax)) %>% 
    dplyr::mutate(lead2_nontax = dplyr::lead(lead1_nontax)) %>% 
    dplyr::mutate(lag1_nontax_ind = ifelse(is.na(lag1_nontax), 0, 1)) %>% 
    dplyr::mutate(lag2_nontax_ind = ifelse(is.na(lag2_nontax), 0, 1)) %>% 
    dplyr::mutate(lead1_nontax_ind = ifelse(is.na(lead1_nontax), 0, 1)) %>% 
    dplyr::mutate(lead2_nontax_ind = ifelse(is.na(lead2_nontax), 0, 1)) %>% 
    dplyr::mutate(nontax_ind = ifelse(is.na(nontax), 0, 1))


dfNontax <- dfNontax %>%
    mutate(row_pres = select(., lag1_nontax_ind:nontax_ind) %>% 
               rowSums(na.rm = TRUE)) %>% 
    dplyr::mutate(row_pres = replace(row_pres, row_pres >= 0, row_pres - ccode)) %>% 
    dplyr::mutate(lag2_nontax = replace(lag2_nontax, is.na(lag2_nontax), 0)) %>% 
    dplyr::mutate(lag1_nontax = replace(lag1_nontax, is.na(lag1_nontax), 0)) %>% 
    dplyr::mutate(nontax = replace(nontax, is.na(nontax), 0)) %>% 
    dplyr::mutate(lead1_nontax = replace(lead1_nontax, is.na(lead1_nontax), 0)) %>% 
    dplyr::mutate(lead2_nontax = replace(lead2_nontax, is.na(lead2_nontax), 0))


dfNontax$nontax <- (dfNontax$lag2_nontax + 
                      dfNontax$lag1_nontax + 
                      dfNontax$nontax + 
                      dfNontax$lead1_nontax + 
                      dfNontax$lead2_nontax)/dfNontax$row_pres
dfNontaxselect <- dfNontax %>% 
    dplyr::mutate(nontax = replace(nontax, is.nan(nontax), NA)) %>% 
    dplyr::select(ccode, 
                  year, 
                  nontax) 

dfNontaxrename <- dfNontaxselect %>% 
    dplyr::rename(nontax = nontax) %>%
    dplyr::ungroup()

dfNontaxclean <- dfNontaxrename %>% 
    dplyr::arrange(desc(ccode)) %>% 
    dplyr::group_by(ccode) %>% 
    dplyr::mutate(lag1_nontax = lag(nontax))

dfMergenmma <- dfMerge %>%
    select(-c(comb_indirect, 
              direct_ex_sc_ex_rt, 
              nontax, 
              rev_ex_gr_ex_sc)) %>% 
    dplyr::rename(orig_acc_vert = acc_vert) %>% 
    dplyr::rename(orig_v2x_corr = v2x_corr) %>% 
    dplyr::rename(orig_comb_indirect = lag1_comb_indirect) %>% 
    dplyr::rename(orig_direct_ex_sc_ex_rt = lag1_direct_ex_sc_ex_rt) %>% 
    dplyr::rename(orig_nontax = lag1_nontax) %>% 
    dplyr::rename(orig_rev_ex_gr_ex_sc = lag1_rev_ex_gr_ex_sc) %>% 
    full_join(dfAccmaclean, by = c("ccode", "year")) %>% 
    full_join(dfV2xcorrclean, by = c("ccode", "year")) %>% 
    full_join(dfCombindirectclean, by = c("ccode", "year")) %>% 
    full_join(dfDirectclean, by = c("ccode", "year")) %>% 
    full_join(dfRevclean, by = c("ccode", "year")) %>% 
    full_join(dfNontaxclean, by = c("ccode", "year")) %>% 
    dplyr::mutate(acc_vert = replace(acc_vert, year > 2016, NA)) %>% 
    dplyr::mutate(v2x_corr = replace(v2x_corr, year > 2016, NA))

dfMergenmmaclean <- dfMergenmma %>% 
    dplyr::mutate(acc_vert = replace(acc_vert, 
                                     year > 2016, 
                                     NA)) %>% 
    dplyr::mutate(acc_vert = ifelse(year == styear & !is.na(orig_acc_vert), 
                                    orig_acc_vert, 
                                    acc_vert)) %>% 
    dplyr::mutate(acc_vert = ifelse(year == 2016 & !is.na(orig_acc_vert), 
                                    orig_acc_vert, 
                                    acc_vert)) %>% 
    dplyr::mutate(v2x_corr = replace(v2x_corr, 
                                     year > 2016, 
                                     NA)) %>% 
    dplyr::mutate(v2x_corr = ifelse(year == styear & !is.na(orig_v2x_corr), 
                                    orig_v2x_corr, 
                                    v2x_corr)) %>% 
    dplyr::mutate(v2x_corr = ifelse(year == 2016 & !is.na(orig_v2x_corr), 
                                    orig_v2x_corr, 
                                    v2x_corr)) %>% 
    dplyr::mutate(lag1_comb_indirect = replace(lag1_comb_indirect, 
                                     year > 2017, 
                                     NA)) %>% 
    dplyr::mutate(lag1_comb_indirect = ifelse(year == styear & !is.na(orig_comb_indirect), 
                                    orig_comb_indirect, 
                                    lag1_comb_indirect)) %>% 
    dplyr::mutate(lag1_comb_indirect = ifelse(year == 2017 & !is.na(orig_comb_indirect), 
                                    orig_comb_indirect, 
                                    lag1_comb_indirect)) %>% 
    dplyr::mutate(lag1_direct_ex_sc_ex_rt = replace(lag1_direct_ex_sc_ex_rt, 
                                     year > 2017, 
                                     NA)) %>% 
    dplyr::mutate(lag1_direct_ex_sc_ex_rt = ifelse(year == styear & !is.na(orig_direct_ex_sc_ex_rt), 
                                    orig_direct_ex_sc_ex_rt, 
                                    lag1_direct_ex_sc_ex_rt)) %>% 
    dplyr::mutate(lag1_direct_ex_sc_ex_rt = ifelse(year == 2017 & !is.na(orig_direct_ex_sc_ex_rt), 
                                    orig_direct_ex_sc_ex_rt, 
                                    lag1_direct_ex_sc_ex_rt)) %>% 
    dplyr::mutate(lag1_nontax = replace(lag1_nontax, 
                                     year > 2017, 
                                     NA)) %>% 
    dplyr::mutate(lag1_nontax = ifelse(year == styear & !is.na(orig_nontax), 
                                    orig_nontax, 
                                    lag1_nontax)) %>% 
    dplyr::mutate(lag1_nontax = ifelse(year == 2017 & !is.na(orig_nontax), 
                                    orig_nontax, 
                                    lag1_nontax)) %>% 
    dplyr::mutate(lag1_rev_ex_gr_ex_sc = replace(lag1_rev_ex_gr_ex_sc, 
                                     year > 2017, 
                                     NA)) %>% 
    dplyr::mutate(lag1_rev_ex_gr_ex_sc = ifelse(year == styear & !is.na(orig_rev_ex_gr_ex_sc), 
                                    orig_rev_ex_gr_ex_sc, 
                                    lag1_rev_ex_gr_ex_sc)) %>% 
    dplyr::mutate(lag1_rev_ex_gr_ex_sc = ifelse(year == 2017 & !is.na(orig_rev_ex_gr_ex_sc), 
                                    orig_rev_ex_gr_ex_sc, 
                                    lag1_rev_ex_gr_ex_sc)) 

numSims <- 4943
depUse <- c("acc_vert", "v2x_corr")
depNames <- c("Vertical Accountability", "Control of Corruption Index")
mainResnmmaclean <- eba_fit(dep.vars = depUse, 
                     dep.names = depNames,
                     samp.num = numSims,
                     data = dfMergenmmaclean)
save(list = c("mainResnmmaclean"), 
     file = paste0("results/main_xnat_nmmacleanresults.RData"))

load("results/main_xnat_nmmacleanresults.RData")
pMainnmmaclean <- ggarrange(mainResnmmaclean$acc_vert$plot, 
                     mainResnmmaclean$v2x_corr$plot, 
                     common.legend = T, legend = "bottom")
pdf(file = paste0("figures/appendix/appendix_fig_c3.pdf"), 
    width = 12, height = 8)
print(pMainnmmaclean)
dev.off() 

